<template>
  <body>
    <div class="page">
      <div class="login-panel">
        <div class="title">
          <span class="big">Mtime</span>
          <span class="small">影片库管理系统</span>
        </div>
        <div class="login-form">
          <div class="form-item">
            <div class="label">登录账户：</div>
            <input
              id="username"
              class="input"
              type="text"
              placeholder="请输入登录账户"
              v-model="username"
            />
            <div class="error">{{ errorText1 }}</div>
          </div>
          <div class="form-item">
            <div class="label">账户密码：</div>
            <input
              id="password"
              class="input"
              type="password"
              placeholder="请输入账户密码"
              v-model="password"
            />
            <div class="error">{{ errorText2 }}</div>
          </div>
          <div class="form-item">
            <div class="login-button" @click="Login()">登录</div>
          </div>
        </div>
      </div>
    </div>
  </body>
</template>
<script>
export default {
  data() {
    return {
      username: "",
      password: "",
      errorText1: "",
      errorText2: "",
    };
  },
  methods: {
    Login() {
      if (this.username == "" || this.password == "") {
        this.errorText1 = "请输入账号!";
        this.errorText2 = "请输入密码!";
        return;
      }

      this.$http
        .request({
          url: "/api/admin/login",
          method: "post",
          params: {
            username: this.username,
            password: this.password,
          },
        })
        .then((response) => {
          console.log("Login响应:", response);
          if (response.status == 200) {
            var idtoken = response.data.id_token;
            console.log(idtoken);
            localStorage.setItem("usertoken", idtoken);
          }
          this.$router.push("/admin?usertoken=" + idtoken);
        })
        .catch((err) => {
          if (err.response.status == 401) {
            console.log("登录失败--账号或密码错误!");
          } else {
            console.log("登录失败--其他错误!");
          }
        });
    },
  },
};
</script>
<style>
body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  padding: 0;
  margin: 0;
}
.page {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4);
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("https://demos.creative-tim.com/light-bootstrap-dashboard-pro-angular2/assets/img/full-screen-image-3.jpg");
  background-repeat: no-repeat;
  background-size: cover;
}
.login-panel {
  background-color: #fff;
  width: 450px;
  height: 400px;
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  align-items: center;
  border-radius: 10px;
}

.login-panel .title {
  margin-top: 20px;
  font-weight: 600;
  color: #fb404b;
}
.title .big {
  font-size: 180%;
}
.title .small {
  color: #888;
  font-size: 120%;
}
.login-form {
  margin-top: 20px;
  width: 90%;
}
.login-form .form-item {
  display: flex;
  flex-direction: column;
  margin-top: 15px;
}
.form-item .label {
  margin-top: 15px;
  margin-bottom: 10px;
  font-size: 110%;
}

.form-item .input {
  line-height: 32px;
  border: none;
  border-bottom: 1px solid #888888;
  outline: none;
  font-size: 105%;
}
.login-button {
  width: 100px;
  padding: 10px 15px;
  text-align: center;
  background-color: #fb404b;
  color: #fff;
  margin: 20px auto;
  cursor: pointer;
}
</style>